package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.event.ShowPubVideoEvent;
import reduce.fz.com.reduce.fragment.SocietyNoticeFragment;
import reduce.fz.com.reduce.fragment.SocietyWorksFragment;
import reduce.fz.com.reduce.util.QuPaiOper;

public class SocietyWorksActivity extends BaseToolBarActivity {


    //社团id
    private Long sid;
    //社团fragment
    private SocietyWorksFragment fragment;
    private static final int REQUEST_CODE_PUB = 1115;

    private QuPaiOper quPaiOper;
    private Video video = null;
    private TextView mToolBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarTitle("社团作品");
        EventBus.getDefault().register(this);
        mToolBtn = setRightBtn(R.drawable.take_video_shape);
        mToolBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pubWork();
            }
        });
        setContentView(R.layout.activity_society_works);
        quPaiOper = QuPaiOper.instance(this, false);
        sid = getIntent().getLongExtra("sid", 0l);
        attachFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(ShowPubVideoEvent event) {
        if (event.card.sid != null && event.card.sid != 0) {
            fragment.addFrist(event.card);
        }
    }

    public void pubWork() {
        quPaiOper.showRecordPage(REQUEST_CODE_PUB);
    }

    @Override
    public void initWidget() {
    }


    @Override
    public void initListener() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_PUB:
                if (data == null) return;
                video = quPaiOper.dealIntent(data);
                forwardEdit(video);
            default:
                break;
        }
    }

    public void forwardEdit(Video video) {
        VideoCard card = new VideoCard();
        card.video = video;
        card.sid = sid;
        Intent intent = new Intent(this, VideoEditDescActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("videocard", card);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void attachFragment() {
        fragment = SocietyWorksFragment.create(sid);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container, fragment).commit();
    }
}
